/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.daydream.serializer.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;

import lombok.Getter;

/**
 * @author william
 * @since 2024/1/1
 */

public class JacksonJson extends AbstractJsonSerializer {
    @Getter
    private final transient ObjectMapper mapper;

    private final transient TypeFactory factory;

    JacksonJson(@Nonnull final ObjectMapper mapper) {
        this.mapper = mapper;
        this.factory = mapper.getTypeFactory();
    }

    @Override
    public <T> String stringify(T object) {
        try {
            return this.mapper.writeValueAsString(object);
        } catch (JsonProcessingException ex) {
            this.rethrowRuntimeException("stringify.mapper.writeValueAsString", ex);
        }
        return null;
    }

    @Override
    public <T> T objectify(String text, Class<T> clz) {
        try {
            return this.mapper.readValue(text, clz);
        } catch (JsonProcessingException ex) {
            this.rethrowRuntimeException("objectify.mapper.readValue", ex);
        }
        return null;
    }

    @Override
    public <T> List<T> objectifyList(String text, Class<T> clz) {
        try {
            return this.mapper.readValue(text, factory.constructCollectionType(List.class, clz));
        } catch (JsonProcessingException ex) {
            this.rethrowRuntimeException("objectifyList.mapper.readValue", ex);
        }
        return null;
    }

    @Override
    public <K, V> Map<K, V> objectifyMap(String text, Class<K> keyClz, Class<V> valClz) {
        try {
            return this.mapper.readValue(text, factory.constructMapType(Map.class, keyClz, valClz));
        } catch (JsonProcessingException ex) {
            this.rethrowRuntimeException("objectifyMap.mapper.readValue", ex);
        }
        return null;
    }

    public JsonNode objectify(String text) {
        try {
            return this.mapper.readTree(text);
        } catch (JsonProcessingException ex) {
            this.rethrowRuntimeException("objectify.mapper.readTree", ex);
        }
        return null;
    }

    @Override
    public <T> byte[] toBytes(T Object) throws IOException {
        return this.mapper.writeValueAsBytes(Object);
    }

    @Override
    public <T> T objectify(byte[] bytes, Class<T> clz) throws IOException {
        return this.mapper.readValue(bytes, clz);
    }

    private void rethrowRuntimeException(String msg, Throwable cause) {
        throw new RuntimeException(msg, cause);
    }
}
